<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="../org/angular.min.js"></script>
</head>
<body ng-app="hd">
<div ng-controller="ctrl">
    <!--ng-click指令可以触发一个函数-->
    商品名称：{{goods.title}}<br/>
    商品价格：{{goods.price}} 元<br/>
    商品数量：{{goods.num}} 件<br/>
    总计：{{goods.price * goods.num}} 元<br/>
    <button ng-click="add()">增加</button>
    <button ng-click="reduce()">减少</button>
    <hr>
    商品名称：{{goodss.data.title}}<br/>
    商品价格：{{goodss.data.price}} 元<br/>
    商品数量：{{goodss.data.num}} 件<br/>
    总计：{{goodss.data.price * goodss.data.num}} 元<br/>
    <button ng-click="goodss.add()">增加</button>
    <button ng-click="goodss.reduce()">减少</button>
</div>


<script>
    var m = angular.module('hd', []);
    m.controller('ctrl', ['$scope', function ($scope) {
        $scope.goods = {'title': 'apple mac', 'price': 300, 'num': 0};
        $scope.add = function () {
            $scope.goods.num = Math.min(++$scope.goods.num, 10);
        }
        $scope.reduce = function () {
            $scope.goods.num = Math.max(--$scope.goods.num, 0);
        }

        $scope.goodss = {
            data: {'title': 'apple mac', 'price': 300, 'num': 0},
            add: function () {
                $scope.goodss.data.num = Math.min(++$scope.goodss.data.num, 10);
            },
            reduce: function () {
                $scope.goodss.data.num = Math.max(--$scope.goodss.data.num, 0);
            }
        }
        ;

    }]);

</script>
</body>
</html>